package creditsProject.client.userOfABank;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import creditsProject.client.manager.ManagerGUI;
import creditsProject.dto.Credit;
import creditsProject.dto.Login;
import creditsProject.dto.RequestDto;
import creditsProject.dto.User;

public class UserGUI extends JFrame {

	private static Login login;
	private static User user;
	private static List<Credit> credits;
	private static List<RequestDto> requests;
	private static String command;
	private static JLabel labelMenu = new JLabel("  ");
	// private JPanel panel = new JPanel();
	private JTextField textFieldPhoneNumber = new JTextField(20);
	private JPasswordField textFieldPassword = new JPasswordField(8);
	private JButton button = new JButton("LOGIN");
	private JButton buttonBye = new JButton("BYE");
	private JLabel labelPhone = new JLabel("Phone number");
	private JLabel labelPassword = new JLabel("Password");
	private static JLabel label = new JLabel("  ");
	private ActionListener myListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				login = new Login(Integer.parseInt(textFieldPhoneNumber
						.getText()), textFieldPassword.getText());

				if (login != null && login.getPass() != null) {
					setLogin(login);
					setCommand("LOGIN");
				} else {
					setLabel("Enter login and password");
				}

			} catch (NumberFormatException nfe) {
				setLabel("Phone should consist of numbers");
			}

		}
	};

	private ActionListener listenerBye = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("bye-bye");
			setCommand("BYE");
			dispose();

		}
	};

	public static String getCommand() {
		return command;
	}

	public static void setCommand(String command) {
		UserGUI.command = command;
	}

	public static Login getLogin() {
		return login;
	}

	public static void setLogin(Login login) {
		UserGUI.login = login;
	}

	public static JLabel getLabel() {
		return label;
	}

	public static void setLabel(String label) {
		UserGUI.label.setText(label);
	}

	public static List<Credit> getCredits() {
		return credits;
	}

	public static void setCredits(List<Credit> credits) {
		UserGUI.credits = credits;
	}

	public static List<RequestDto> getRequests() {
		return requests;
	}

	public static void setRequests(List<RequestDto> requests) {
		UserGUI.requests = requests;
	}
	
	public static JLabel getLabelMenu() {
		return labelMenu;
	}

	public static void setLabelMenu(String labelMenu) {
		UserGUI.labelMenu.setText(labelMenu);
		}

	public UserGUI() {
		setTitle("User");
		this.setSize(500, 250);
		this.setLocation(200, 200);
		Box panel = Box.createVerticalBox();
		this.add(panel);
		panel.add(labelPhone);
		labelPhone.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(textFieldPhoneNumber);
		textFieldPhoneNumber.setHorizontalAlignment(JTextField.CENTER);
		textFieldPhoneNumber.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(labelPassword);
		labelPassword.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(textFieldPassword);
		textFieldPassword.setHorizontalAlignment(JTextField.CENTER);
		textFieldPassword.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(button);
		button.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(buttonBye);
		buttonBye.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(label);
		label.setAlignmentX(CENTER_ALIGNMENT);
		panel.add(Box.createVerticalStrut(10));

		button.addActionListener(myListener);
		buttonBye.addActionListener(listenerBye);

	}

	private JButton buttonShowAvailiableCredits = new JButton(
			"Availiable Credits");
	private JButton buttonHistory = new JButton("History");

	private ActionListener showCredits = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			setCommand("GETAVALCREDITS");

		}
	};

	private ActionListener showHistory = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			setCommand("GETHISTORY");

		}
	};

	public void UserMenu() {
		this.setSize(500, 250);
		this.setLocation(200, 200);
		Box panel = Box.createVerticalBox();
		this.add(panel);

		panel.add(Box.createVerticalStrut(10));
		panel.add(buttonShowAvailiableCredits);
		buttonShowAvailiableCredits.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(buttonHistory);
		buttonHistory.setAlignmentX(CENTER_ALIGNMENT);

		panel.add(Box.createVerticalStrut(10));
		panel.add(buttonBye);
		buttonBye.setAlignmentX(CENTER_ALIGNMENT);
		panel.add(Box.createVerticalStrut(20));
		panel.add(panLable);
		panLable.setAlignmentX(CENTER_ALIGNMENT);
		panLable.add(labelMenu);
		panel.add(Box.createVerticalStrut(10));

		// buttonBye.addActionListener(listenerBye);
		buttonHistory.addActionListener(showHistory);
		buttonShowAvailiableCredits.addActionListener(showCredits);

	}
	private JPanel panLable = new JPanel(); 
	private static HashSet<Integer> idCredits = new HashSet<>();

	public static HashSet<Integer> getIdCredits() {
		return idCredits;
	}

	public static void setIdCredits(HashSet<Integer> idCredits) {
		UserGUI.idCredits = idCredits;
	}
	
	public static void clearIdCredits() {
		UserGUI.idCredits.clear();
	}

	private JButton requestButton = new JButton("Request");

	public void ShowCredits() {

		this.setSize(500, 250);
		this.setLocation(700, 200);
		Box CreditsPanel = Box.createVerticalBox();
		this.add(CreditsPanel);

		DefaultTableModel model = new DefaultTableModel(null, new String[] {
				"Check", "Id", "Credit Name", "Description", "Duration" }) {
			public Class getColumnClass(int c) {
				switch (c) {
				case 0:
					return Boolean.class;
				default:
					return String.class;
				}
			}
			public boolean isCellEditable(int row, int column)
		    {
			  if(column == 0){
				  return true;
			  }else{   return false;}
		    }
		};

		String description = "";
		String name = "";
		String duration = "";
		int creditId = 0;
		Vector columnData = new Vector<>();
		Vector vecData = new Vector<>();
		JCheckBox chkBox = new JCheckBox();
		for (int i = 0; i < credits.size(); i++) {
			columnData = new Vector<>();
			creditId = credits.get(i).getCreditId();
			name = credits.get(i).getName();
			description = credits.get(i).getDescription();
			duration = credits.get(i).getDuration();

			columnData.add(Boolean.FALSE);
			columnData.add(new Integer(creditId));
			columnData.add(new String(name));
			columnData.add(new String(description));
			columnData.add(new String(duration));
			model.addRow(columnData);
		}

		JTable tableCredits = new JTable(model);
		tableCredits.getModel().addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e1) {

				int row = e1.getFirstRow();
				int column = e1.getColumn();
				DefaultTableModel model = (DefaultTableModel) e1.getSource();
				String columnName = model.getColumnName(column);
				Boolean data = (Boolean) model.getValueAt(row, column);
				if (data == true) {
					Integer id = (Integer) model.getValueAt(row, column + 1);
					idCredits.add(id);
				}
			}
		});
		CreditsPanel.add(new JScrollPane(tableCredits), BorderLayout.CENTER);
		CreditsPanel.validate();

		CreditsPanel.add(Box.createVerticalStrut(10));
		CreditsPanel.add(requestButton);
		requestButton.setAlignmentX(CENTER_ALIGNMENT);
		requestButton.addActionListener(requestListener);

		CreditsPanel.add(Box.createVerticalStrut(10));
		CreditsPanel.add(label);

	}

	private ActionListener requestListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			setCommand("CREATEREQUEST");
			setLabelMenu("Request sent to manager");
			dispose();

		}
	};

	public void ShowHistory() {

		this.setSize(600, 250);
		this.setLocation(700, 200);
		Box historyPanel = Box.createVerticalBox();
		this.add(historyPanel);

		DefaultTableModel model = new DefaultTableModel(null, new String[] {
				"Date", "Request id", "Credit Name", "Approved", "Declined" }) {
			public Class getColumnClass(int c) {
				switch (c) {
				case 4:
					return Boolean.class;
				case 3:
					return Boolean.class;
				default:
					return String.class;
				}
			}
			public boolean isCellEditable(int row, int column)
		    {
		      return false;
		    }
			
		};

		String date = "";
		int requestId = 0;
		String name = "";

		Vector columnData = new Vector<>();
		Vector vecData = new Vector<>();
		JCheckBox chkBox = new JCheckBox();
		for (int i = 0; i < requests.size(); i++) {
			columnData = new Vector<>();
			columnData.add(new String(requests.get(i).getDate()));
			columnData.add(new Integer(requests.get(i).getRequestId()));
			columnData.add(new String(requests.get(i).getCreditName()));
			columnData.add(new Boolean(requests.get(i).isApproved()));
			columnData.add(new Boolean(requests.get(i).isDeclined()));
			model.addRow(columnData);
		}

		JTable tableCredits = new JTable(model);

		historyPanel.add(new JScrollPane(tableCredits), BorderLayout.CENTER);
		historyPanel.validate();

		historyPanel.add(Box.createVerticalStrut(10));
		historyPanel.add(label);

	}

}
